/**
 * AudioUploadDAO.java
 * Creates a data access object (DAO) for an audio data transfer object (DTO).
 * @author Sejung
 */
package com.example.myproject;

import java.util.List;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.DAOBase;

public class AudioUploadDAO extends DAOBase{

    static {
        ObjectifyService.register(AudioUploadDTO.class);
    }
    
    /**
     * Saves an AudioUploadDTO to the blobstore.
     * @param dto The audio object to be saved.
     */  
    public AudioUploadDTO save(AudioUploadDTO dto) {
        ofy().put(dto);
        return dto;
    }
 
    /**
     * Retrieves AudioUploadDTOs from the blobstore with matching IDs.
     * @param id Supplied search parameter.
     * @return Returns a key map of AudioUploadDTOs matching id.
     */	
    public AudioUploadDTO findById(Long id) {
    	if(id <= 1L) return null;
        Key<AudioUploadDTO> key = new Key<AudioUploadDTO>(AudioUploadDTO.class, id);
        return ofy().get(key);
    }
  
    /**
     * Retrieves a list of all AudioUploadDTOs in the blobstore.
     * @return List containing all AudioUploadDTOs in the blobstore.
     */    
	public List<AudioUploadDTO> list() 
	{	
		List<AudioUploadDTO> alist = ofy().query(AudioUploadDTO.class).list();
		return alist;
	}

    /**
     * Retrieves a list of all AudioUploadDTOs in the blobstore whose genre
     * matches the supplied string.
     * @param genre Supplied search parameter.
     * @return List containing AudioUploadDTOs with matching genres.
     */		
	public List<AudioUploadDTO> listByGenre(String genre) 
	{		
		List<AudioUploadDTO> alist = ofy().query(AudioUploadDTO.class).filter("genre", genre).list();
		return alist;
	}

    /**
     * Deletes an AudioUploadDTO from the blobstore, specified by id.
     * @param id Supplied id of the AudioUploadDTO to be deleted.
     */
	public void delete(Long id) 
	{
		if(id <= 1L) return ;
		AudioUploadDTO audio = ofy().query(AudioUploadDTO.class).filter("id ", id).get();
		ofy().delete(audio);
		
	}	
	
    /**
     * Deletes an AudioUploadDTO from the blobstore, specified by parentId.
     * @param parentId Supplied parentId of the AudioUploadDTO to be deleted.
     */
	public void deleteByParentId(Long parentId) 
	{		
		if(parentId <= 1L) return ;
		AudioUploadDTO audio = ofy().query(AudioUploadDTO.class).filter("parentId ", parentId).get();
		ofy().delete(audio);
	}
	
}
